package com.isoftstone.util.docment;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @描述: 利用dom4j读取xml文件
 * @描述: 使用iterator迭代器的方式来解析xml文件
 * @author: jgzhao
 * @date: 2017/6/27
 */
public class DocumentReader {

    public static void main(String[] args) {

        String xml = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
                "<UTILITY_PAYMENT>\n" +
                "\t<CONST_HEAD>\n" +
                "\t\t<REQUEST_TYPE>0200</REQUEST_TYPE>\n" +
                "\t\t<CHANNEL_ID>BOCSDOPN</CHANNEL_ID>\n" +
                "\t\t<FRONT_TRACENO>201510270000917</FRONT_TRACENO>\n" +
                "\t\t<CSPS_TRACENO>00132734</CSPS_TRACENO>\n" +
                "\t\t<TRN_CODE>mobile02</TRN_CODE>\n" +
                "\t\t<ERR_CODE>00</ERR_CODE>\n" +
                "\t\t<ERR_MSG>交易成功[0000000]</ERR_MSG>\n" +
                "\t</CONST_HEAD>\n" +
                "\t<DATA_AREA>\n" +
                "\t\t<PAY_AMOUNT>3200.00</PAY_AMOUNT>\n" +
                "\t\t<PAY_DATE>20180502</PAY_DATE>\n" +
                "\t\t<PAY_TIME>102442</PAY_TIME>\n" +
                "\t\t<PAY_TRACENO>00132734</PAY_TRACENO>\n" +
                "\t</DATA_AREA>\n" +
                "</UTILITY_PAYMENT>";
        Map<String, String> stringStringMap = xmlReaderMessage(xml);
        String request_type = stringStringMap.get("REQUEST_TYPE");
        System.out.println("request_type = " + request_type);
    }

    public static Map<String, String> xmlReaderMessage(String xmlStr) {

        Map<String, String> map = new HashMap<>();

        try {
            Document dom = DocumentHelper.parseText(xmlStr);

            Element root = (Element) dom.getRootElement();

            Iterator iterator1 = root.elementIterator();
            while (iterator1.hasNext()) {
                Element e = (Element) iterator1.next();
                //获取元素名称
                String parentName = e.getName();
                if (parentName.equals("CONST_HEAD")) {
                    Iterator iterator3 = e.elementIterator();
                    while (iterator3.hasNext()) {
                        Element ee = (Element) iterator3.next();
                        String childName = ee.getName();
                        if (childName.equals("REQUEST_TYPE")) { //请求类型
                            String request_type = ee.getText();
                            map.put("REQUEST_TYPE",request_type);
                        }
                        if (childName.equals("CHANNEL_ID")) { //交易渠道
                            String channel_id = ee.getText();
                            map.put("CHANNEL_ID",channel_id);
                        }
                        if (childName.equals("FRONT_TRACENO")) { //快缴交易流水号
                            String front_traceno = ee.getText();
                            map.put("FRONT_TRACENO",front_traceno);
                        }
                        if (childName.equals("CSPS_TRACENO")) { //CSP交易流水号
                            String csps_traceno = ee.getText();
                            map.put("CSPS_TRACENO",csps_traceno);
                        }
                        if (childName.equals("TRN_CODE")) { //CSP交易码
                            String trn_code = ee.getText();
                            map.put("TRN_CODE",trn_code);
                        }
                        if (childName.equals("ERR_CODE")) { //CSP交易状态码
                            String err_code = ee.getText();
                            map.put("ERR_CODE",err_code);
                        }
                        if (childName.equals("ERR_MSG")) { //CSP交易结果提示
                            String err_msg = ee.getText();
                            map.put("ERR_MSG",err_msg);
                        }
                    }
                }
                if (parentName.equals("DATA_AREA")) {
                    Iterator iterator3 = e.elementIterator();
                    while (iterator3.hasNext()) {
                        Element ee = (Element) iterator3.next();
                        String childName = ee.getName();
                        if (childName.equals("PAY_AMOUNT")) { //扣款金额
                            String pay_amount = ee.getText();
                            map.put("PAY_AMOUNT",pay_amount);
                        }
                        if (childName.equals("PAY_DATE")) { //扣款成功日期
                            String pay_date = ee.getText();
                            map.put("PAY_DATE",pay_date);
                        }
                        if (childName.equals("PAY_TIME")) { //扣款成功日期
                            String pay_time = ee.getText();
                            map.put("PAY_TIME",pay_time);
                        }
                        if (childName.equals("PAY_TRACENO")) { //CSP交易流水号
                            String pay_traceno = ee.getText();
                            map.put("PAY_TRACENO",pay_traceno);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}

